package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.common.constants.DBConstants;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.favorites.model.vo.FavoritesIntroVO;
import org.fuys.coder.domain.favorites.repository.IFavoriteRepository;
import org.fuys.coder.infrastructure.dao.FavoriteMapper;
import org.fuys.coder.infrastructure.dao.SubstanceFavoritesMapper;
import org.fuys.coder.infrastructure.model.po.Favorites;
import org.fuys.coder.infrastructure.model.po.SubstanceFavorites;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: IFavoriteRepositoryImpl
 * @author: WangJing
 * @description:
 * @date: 2024/6/19 18:13
 * @version: 1.0
 */
@Service
public class FavoriteRepositoryImpl extends ServiceImpl<FavoriteMapper, Favorites>
        implements IFavoriteRepository {

    @Resource
    private FavoriteMapper favoriteMapper;

    @Resource
    private SubstanceFavoritesMapper substanceFavoritesMapper;

    public long insertFavorite(Favorites favorites)
    {
        return this.favoriteMapper.insert(favorites);
    }

    @Override
    public long createNewEmptyFavorite() {
        Favorites favorites=new Favorites();
        favorites.setTitle(DBConstants.FAVORITES_NAME);
        return this.favoriteMapper.insert(favorites);
    }

    @Override
    public Pair<List<Long>, Long> selectContentIdPageByFavId(Long favId, Integer curPage, Integer pageNum) {
        Page<SubstanceFavorites> page=new Page<>(curPage,pageNum);
        final Page<SubstanceFavorites> substanceFavoritesPage = substanceFavoritesMapper.
                selectPage(page, new LambdaQueryWrapper<SubstanceFavorites>()
                .select(SubstanceFavorites::getSubstanceId).eq(SubstanceFavorites::getFavId, favId));
        if(ObjectUtils.isEmpty(substanceFavoritesPage)||ObjectUtils.isEmpty(substanceFavoritesPage.getRecords())){
            return null;
        }
        final List<Long> collect = page.getRecords().stream().map(SubstanceFavorites::getSubstanceId).collect(Collectors.toList());
        return new Pair<>(collect,substanceFavoritesPage.getTotal());
    }

    @Override
    public List<FavoritesIntroVO> selectFavInfo(List<Long> ids, boolean equals) {
        final List<Favorites> favorites = favoriteMapper.selectList(new LambdaQueryWrapper<Favorites>()
                .select(Favorites::getTitle, Favorites::getDescription, Favorites::getId)
                .in(Favorites::getId, ids).eq(!equals, Favorites::getIsOpen,1));
        return favorites.stream().map(item -> {
            FavoritesIntroVO vo = new FavoritesIntroVO();
            vo.setId(item.getId());
            vo.setName(item.getTitle());
            vo.setDesc(item.getDescription());
            return vo;
        }).collect(Collectors.toList());
    }
}
